<?php
/**
 * User: @fs
 * Date: 2017/9/10
 * Time: 18:32
 */

namespace core;

use core\lib\log;

class imooc {

    static  public  function run(){
        //p('ok');
        $route = new \core\lib\route();
        /**
         * 1.开始加载控制器 , 引入对应文件,是否存在，存在的话 输出对应控制器方法
         */
        $ctrlClass = $route->ctrl;
        $action = $route->action;
        $file = "\\".MODEL.'\index\\'.$ctrlClass;
        $ctrl = new $file();
        $ctrl->$action();

        //输出log
        log::init();
        log::log($_SERVER['REDIRECT_URL']);

    }
    static public function  load($class){
       //core\route

        $file= IMOOC.'/'.$class.'.php';

        if(is_file($file)){
            include $file;

        }else{
           throw new \Exception("{$class} not exits!");
        }
    }

    public  $arr= array();

    public function assgin($name,$value){
        $this->arr[$name]=$value;
    }

    public function display($file){
        $path=APP."/view/$file";
        $dirarr = pathinfo($path);
        $dir = $dirarr['dirname'];
        $fiename=$dirarr['basename'];

        if(is_file($path)){
            

            $loader = new \Twig_Loader_Filesystem($dir);
            $twig = new \Twig_Environment($loader, array(
                'cache' => IMOOC.'/log',
                'debug'=>DEBUG
            ));
            $template = $twig->load($fiename);
            $template->display($this->arr);

        }else{
            throw new \Exception("没有找到试图目录".$file);
        }

    }

}